home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_execfile.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.0 KB  |  60 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer for execfile.
  5.  
  6. This converts usages of the execfile function into calls to the built-in
  7. exec() function.
  8. '''
  9. from  import fixer_base
  10. from fixer_util import Comma, Name, Call, LParen, RParen, Dot, Node, ArgList, String, syms
  11.  
  12. class FixExecfile(fixer_base.BaseFix):
  13.     PATTERN = "\n    power< 'execfile' trailer< '(' arglist< filename=any [',' globals=any [',' locals=any ] ] > ')' > >\n    |\n    power< 'execfile' trailer< '(' filename=any ')' > >\n    "
  14.     
  15.     def transform(self, node, results):
  16.         if not results:
  17.             raise AssertionError
  18.         filename = results['filename']
  19.         globals = results.get('globals')
  20.         locals = results.get('locals')
  21.         execfile_paren = node.children[-1].children[-1].clone()
  22.         open_args = ArgList([
  23.             filename.clone()], rparen = execfile_paren)
  24.         open_call = Node(syms.power, [
  25.             Name('open'),
  26.             open_args])
  27.         read = [
  28.             Node(syms.trailer, [
  29.                 Dot(),
  30.                 Name('read')]),
  31.             Node(syms.trailer, [
  32.                 LParen(),
  33.                 RParen()])]
  34.         open_expr = [
  35.             open_call] + read
  36.         filename_arg = filename.clone()
  37.         filename_arg.set_prefix(' ')
  38.         exec_str = String("'exec'", ' ')
  39.         compile_args = open_expr + [
  40.             Comma(),
  41.             filename_arg,
  42.             Comma(),
  43.             exec_str]
  44.         compile_call = Call(Name('compile'), compile_args, '')
  45.         args = [
  46.             compile_call]
  47.         if globals is not None:
  48.             args.extend([
  49.                 Comma(),
  50.                 globals.clone()])
  51.         
  52.         if locals is not None:
  53.             args.extend([
  54.                 Comma(),
  55.                 locals.clone()])
  56.         
  57.         return Call(Name('exec'), args, prefix = node.get_prefix())
  58.  
  59.  
  60.